import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Controls from '#cpt/temp/Controls'
import TableControls from '#cpt/temp/TableControls'
import Enable from '#cpt/temp/Enable'
import Delete from '#cpt/temp/Delete'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 					= $Lazy.load(()=>import('#tp/page/page-container'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 搜索表单列表
		forms : [
			{ label: '模板编号',		name: 'lab_number' },
			{ label: '模板名称',		name: 'lab_name' },
		],
	}
	
	model = {}
	
	componentDidMount(){
		if(window.isInit){
			this.fetch()
		}
		$fn.fresh.on.call(this, this.fetch)
	}
	
	componentWillUnmount(){
		$fn.fresh.remove()
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'lis-lab-tag/index')
	
	// 表单
	cols = [
		{ title: '模板编号', 	dataIndex: 'lab_number', 	width:150 },
		{ title: '模板名称', 	dataIndex: 'lab_name', 		width:200 },
		{ title: '当前实验号', 	dataIndex: 'last_code', 	width:150 },
		{ title: '结束时间', 	dataIndex: 'cycle_end', 	...$fn.table.time },
		{ title: '状态', 		dataIndex: 'enabled', 		...$fn.table.status, render: text => +text === 1 ? '启用' : '禁用' },
		{ title: '描述', 		dataIndex: 'description', 	width:200  },
		TableControls.controls.call(this, { width: 180 })
	]
	// 表格操作按钮
	table_controls = (text,{uuid, enabled}) => {
		return [
			TableControls.pushTo.call(this, '编辑', `template-add?uuid=${uuid}`),
			this.onDel([ uuid ]),														// 删除
			+enabled === 0 && this.onEnabled(1, [ uuid ]),							// 启用
			+enabled === 1 && this.onEnabled(0, [ uuid ]),							// 禁用
		]
	}
	// 操作按钮
	controls = () => [
		// 添加
		Controls.pushTo.call(this, '设置规则', 'template-add', { code:'F1'} ),
		this.onDel(),		// 删除
		this.onEnabled(0),	// 禁用
		this.onEnabled(1),	// 启用
	]
	
	// 删除
	onDel = uuid => Delete.call(this, 'lis-lab-tag/delete', { uuid, isKeys: !uuid, onComplete:({ param })=>{$fn.dataClear()} })
	
	// 禁用/启用
	onEnabled = (enabled, uuid) => Enable.call(this, enabled,  { uuid, on:'lis-lab-tag/on', off:'lis-lab-tag/off', isKeys: !uuid, onComplete:({ param })=>{$fn.dataClear()} })
	
	render(){
		return (
			<Page title='实验号模板' controls={this.controls()}>
				{/* 搜索 */}
				{ SearchForm.call(this) }
				{/* 表格 */}
				{ Table.call(this,{isInit:window.isInit}) }
			</Page>
		)
	}
}

export default Index